<template>
	<view>
		<text class="su-numtext" :style="{color:textColor}" 
			  @click="_openView">{{getCount(text)}}</text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		props: {
			attachmentList: {
				type: [String,Array] //附件列表
			},
			text:{
				type:String,
				default: '{0}'
			},
			textColor:{
				type:String,
				default: ''
			},
			canDownload:{
				type:Boolean,
				default:true
			}
		},
		computed: {
			count() {
				var objType = typeof this.attachmentList
				if(objType == 'string') {
					 let mdata = []
					 try{
					 	mdata = JSON.parse(this.attachmentList)
					 }catch(e){
					 	//TODO handle the exception
					 }
					 return mdata.length
				}else {
					if (objType == 'undefined') return 0
					if (objType == 'object') {
						if(this.attachmentList) return this.attachmentList.length
						else return 0
					}
				}
				return 0
			},
			getCount(value){
				return function(value){
					return uni.core.strFormat(value,this.count)
				}
			}
		},
		methods:{
			_openView(){
				var objType = typeof this.attachmentList
				var openData = {
					canDownload:this.canDownload,
					attData:[]
				}
				if(objType == 'string') {
					try{
						openData.attData =JSON.parse(this.attachmentList)
					}catch(e){
						//TODO handle the exception
					}
				}else {
					if (objType != 'undefined' && objType == 'object') {
						openData.attData = this.attachmentList
					}
				}
				if(openData.attData.length==0) 
					return uni.core.toast("暂无可预览的附件")
				uni.setStorageSync("attView", JSON.stringify(openData))
				uni.core.openPage("/components/su-system/attachment/view-list")
			}
		}
	}
</script>

<style lang="scss">
	.su-numtext {
		color: $maincolor;
		font-size: 30upx;
	}
</style>
